package com.yxt.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yxt.common.sys.CommConstant;
import com.yxt.common.ui.ActivityStackControlUtil;
import com.yxt.common.ui.NormalTableAdapter;
import com.yxt.common.ui.NormalTableAdapter.TableCell;
import com.yxt.common.ui.NormalTableAdapter.TableRow;
import com.yxt.model.Address;
import com.yxt.service.AddressService;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.LinearLayout.LayoutParams;
/**
 * 通讯录查询
 * @author TYP
 *
 */
public class AddressSearchActivity extends Activity {
	private int height = CommConstant.screenHeight / 8;
	private int color = Color.WHITE;
	private AddressService service = new AddressService(AddressSearchActivity.this);
	private RadioButton radioSearchLxr;
	private RadioButton radioSearchDept;
	private RadioButton radioSearchDwzw;
	private RadioButton radioSearchFbzw;
	private EditText txtValue;
	private Button btnClear;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
		setContentView(R.layout.address_search);//软件activity的布局
		ActivityStackControlUtil.add(this);
		initComponent();
	}
	
	private void initComponent(){
		btnClear = (Button)findViewById(R.id.btnClear);
		txtValue = (EditText)findViewById(R.id.txtValue);
		radioSearchLxr = (RadioButton)findViewById(R.id.radioSearchLxr);
		radioSearchDept = (RadioButton)findViewById(R.id.radioSearchDept);
		radioSearchDwzw = (RadioButton)findViewById(R.id.radioSearchDwzw);
		radioSearchFbzw = (RadioButton)findViewById(R.id.radioSearchFbzw);
		
		btnClear.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				txtValue.setText("");
			}
		});		
		txtValue.addTextChangedListener(new TextWatcher(){             
            public void afterTextChanged(Editable s) { 
            } 

            public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
            } 
 
            public void onTextChanged(CharSequence s, int start, int before, int count) { 
            	String key = "";
            	if (radioSearchLxr.isChecked()) {
            		key = CommConstant.ADDRESS_MAP.get(radioSearchLxr.getText().toString());
		    	} else if (radioSearchDept.isChecked()) {
		    		key = CommConstant.ADDRESS_MAP.get(radioSearchDept.getText().toString());
		    	} else if (radioSearchDwzw.isChecked()) {
		    		key = CommConstant.ADDRESS_MAP.get(radioSearchDwzw.getText().toString());
		    	} else if (radioSearchFbzw.isChecked()) {
		    		key = CommConstant.ADDRESS_MAP.get(radioSearchFbzw.getText().toString());
		    	}
            	String value = s.toString();
				fillAddressList(getSearchList(key,value));
            } 
        });
		
		Button btnBackHome = (Button)findViewById(R.id.btnBackHome);
		btnBackHome.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), MainActivity.class);
				startActivity(intent);
			}
		});
	}
	
	private void fillAddressList(List<Address> list){
		List<TableRow> table = new ArrayList<TableRow>();     
        int[] width = {250,50};
		// 每行的数据  
        Map<String,String> map = new HashMap<String,String>();
        for (int i = 0; i < list.size(); i++) {
        	Address addr = list.get(i);
        	String city = addr.getStb_pcode();
        	if (!map.containsKey(city)) {
        		TableCell[] cells = new TableCell[1];
                cells[0] = new TableCell(city, LayoutParams.FILL_PARENT, 40, Color.GRAY, TableCell.DISABLE);  
                table.add(new TableRow(cells)); 
        		map.put(city, city); 
        	}
        	TableCell[] cells = new TableCell[1];
        	cells[0] = new TableCell("  " + addr.getName()+ "■" + addr.getJob(), width[0], height, color, TableCell.STRING, new TextItemOnClickListener(addr.getId())); 
        	table.add(new TableRow(cells)); 
        }
        NormalTableAdapter tableAdapter = new NormalTableAdapter(this, table);   
        ListView lstWaterStation = (ListView)findViewById(R.id.lstAddressList);
        lstWaterStation.setAdapter(tableAdapter);
	}
	
	private List<Address> getSearchList(String key,String value){
		List<Address> list = new ArrayList<Address>();
		list.addAll(service.getAddressListByCondition(key, value));
		return list;
	}
	
	private class TextItemOnClickListener implements OnClickListener{
		private String addrId;
		public TextItemOnClickListener(String addrId){	
			this.addrId = addrId;
		}
		
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), AddressDetailActivity.class);
			intent.putExtra("addrId", addrId);
			startActivity(intent);
		}
		
	}

	protected void onDestroy() {
		super.onDestroy();
		service.closeDB();
        ActivityStackControlUtil.remove(this); 
	}
}